/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.cef.command;

import cef.command.Command;
import cef.command.CommandException;
import genomemap.cef.event.ClassifyGenes_Evt;
import genomemap.data.OrganismData;

import java.util.*;

/**
 * @author Susanta Tewari
 * @version 1.0 Nov 21, 2010
 */
public class ClassifyGenes_Cmd extends Command<ClassifyGenes_Evt> {

    /** Field description */
    private Map<Integer, Set<String>> map;

    public ClassifyGenes_Cmd(final ClassifyGenes_Evt event) {
        super(event);
    }

    @Override
    public void executeImpl() throws CommandException {


        // 2.  get the input genes
        final Set<String> inputGenes = getEvent().getInputGenes();


        // 3. get an instance of the associated organism data
        final OrganismData inputData = getEvent().getOrganismDataUsed();


        // 4. create the classified genes
        // 4.1 init the map
        map = new HashMap<>();

        for (final Integer linkageGroup : inputData.getLinkageGroups()) {
            map.put(linkageGroup, new HashSet<String>());
        }


        // 4.2 init genes_not_found

        /*
         * pool of genes from the input not present in the associated organism data instance
         */
        final Set<String> genes_not_found = new HashSet<>();


        // 4.3 process every input gene against the genes from organism data
        // If found, put in the map else in the not found pool
        for (final String gene : inputGenes) {

            for (final Integer linkageGroup : inputData.getLinkageGroups()) {

                final List<String> genes = inputData.genes(linkageGroup);

                if (genes.contains(gene)) {
                    map.get(linkageGroup).add(gene);
                } else {
                    genes_not_found.add(gene);
                }
            }
        }
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public Map<Integer, Set<String>> getClassifiedGenes() {

        checkCmdFinished();

        return map;
    }
}
